package com.vshop.platform.system.crud.dao;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vshop.platform.system.domain.entity.SysClient;
import com.vshop.platform.system.crud.mapper.SysClientMapper;
import com.vshop.platform.system.domain.param.SysClientParam;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.vshop.platform.system.domain.entity.table.SysClientTableDef.SysClientTable;


/**
 * 系统授权表 服务层实现。
 *
 * @author vshop
 * @since 2024-11-18
 */
@RequiredArgsConstructor
@Slf4j
@Repository
public class SysClientDao extends ServiceImpl<SysClientMapper, SysClient> {

    public QueryWrapper buildQueryWrapper(SysClientParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.from(SysClientTable)
                .and(SysClientTable.ClientId.eq(param.getClientId()))
                .and(SysClientTable.ClientKey.eq(param.getClientKey()))
                .and(SysClientTable.ClientSecret.eq(param.getClientSecret()))
                .and(SysClientTable.Status.eq(param.getStatus()))
                .orderBy(SysClientTable.Id.asc());
        return queryWrapper;
    }

    public boolean changeStatus(Long id, String status) {
        return this.updateChain()
                .from(SysClientTable)
                .set(SysClientTable.Status, status)
                .where(SysClientTable.Id.eq(id))
                .update();
    }

    public boolean checkExistClientKey(String clientKey) {
        return this.queryChain().from(SysClientTable)
                .where(SysClientTable.ClientKey.eq(clientKey))
                .exists();
    }

    public SysClient getByClientId(String clientId) {
        return this.queryChain().from(SysClientTable)
                .where(SysClientTable.ClientId.eq(clientId))
                .one();
    }
}
